﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using Model;
using Model.EntityModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for SearchComplaint.xaml
    /// </summary>
    public partial class SearchComplaint : UserControl
    {
        public List<ShowablePerson> Persons { get; set; }
        public long SelectedComplaintId { get; set; }
        public string VictimId { get; set; }
        public string AggressorId { get; set; }
        public string ComplainantId { get; set; }
        private List<ComplaintInformation> complaints; 
        public event RoutedEventHandler getComplaint;

        public SearchComplaint()
        {
            InitializeComponent();
            rbtnByName.IsChecked = true;
            dtStart.DisplayDateEnd = DateTime.Now;
            dtStart.SelectedDate = null;
            dtStart.DisplayDateStart = DateTime.Now.AddYears(-10);
            dtEnd.DisplayDateEnd = DateTime.Now;
            dtEnd.DisplayDateStart = DateTime.Now.AddYears(-10);
            dtEnd.SelectedDate = DateTime.Now;
            Persons = PersonAndActorDataAccess.GetAllShowablePersons();
            tbVictimName.ItemsSource = Persons;
            tbVictimCi.ItemsSource = Persons;
            tbAggressorName.ItemsSource = Persons;
            tbAggressorCi.ItemsSource = Persons;
            tbCopmlainantName.ItemsSource = Persons;
            tbComplainantCi.ItemsSource = Persons;
            complaints = ComplaintDataAccess.GetAllComplaintsInformation();
            deleteInconclusiveComplaints();
            tbCaseNumber.ItemsSource = complaints;
            listViewComplaints.DataContext = complaints;

        }

        private void deleteInconclusiveComplaints()
        {
            List<ComplaintInformation> res = new List<ComplaintInformation>();
            foreach (var complaint in complaints)
            {
                if(complaint.VictimCi!="0")
                    res.Add(complaint);
            }
            complaints = res;
        }

        private void FilterResults()
        {
            if (complaints != null)
            {
                listViewComplaints.DataContext = ComplaintDataAccess.GetComplaintsBy(complaints,VictimId, AggressorId, ComplainantId,dtStart.SelectedDate,dtEnd.SelectedDate,tbCaseNumber.Text);
            }
        }

        private void NameDropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            if (rbtnByName.IsChecked == true)
            {
                ShowablePerson selectedPerson = tbVictimName.SelectedItem as ShowablePerson;
                if (selectedPerson != null && tbVictimName.IsEnabled)
                {
                    tbVictimCi.Text = selectedPerson.CiPassport.ToString();
                    VictimId = selectedPerson.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson2 = tbAggressorName.SelectedItem as ShowablePerson;
                if (selectedPerson2 != null && tbAggressorName.IsEnabled)
                {
                    tbAggressorCi.Text = selectedPerson2.CiPassport.ToString();
                    AggressorId = selectedPerson2.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson3 = tbCopmlainantName.SelectedItem as ShowablePerson;
                if (selectedPerson3 != null && tbCopmlainantName.IsEnabled)
                {
                    tbComplainantCi.Text = selectedPerson3.CiPassport.ToString();
                    ComplainantId = selectedPerson3.Id;
                    FilterResults();
                }
            }
        }

        private void CiDropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            if (rbtnByCi.IsChecked==true)
            {
                ShowablePerson selectedPerson = tbVictimCi.SelectedItem as ShowablePerson;
                if (selectedPerson != null && tbVictimCi.IsEnabled)
                {
                    tbVictimName.Text = selectedPerson.FullName;
                    VictimId = selectedPerson.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson2 = tbAggressorCi.SelectedItem as ShowablePerson;
                if (selectedPerson2 != null && tbAggressorCi.IsEnabled)
                {
                    tbAggressorName.Text = selectedPerson2.FullName;
                    AggressorId = selectedPerson2.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson3 = tbComplainantCi.SelectedItem as ShowablePerson;
                if (selectedPerson3 != null && tbComplainantCi.IsEnabled)
                {
                    tbCopmlainantName.Text = selectedPerson3.FullName;
                    ComplainantId = selectedPerson3.Id;
                    FilterResults();
                }
            }
        }

        private void rbtnByName_Checked(object sender, RoutedEventArgs e)
        {
            tbVictimName.IsEnabled = true;
            tbAggressorName.IsEnabled = true;
            tbCopmlainantName.IsEnabled = true;
            tbAggressorCi.IsEnabled = false;
            tbVictimCi.IsEnabled = false;
            tbComplainantCi.IsEnabled = false;
        }

        private void rbtnByCi_Checked(object sender, RoutedEventArgs e)
        {
            tbVictimName.IsEnabled = false;
            tbAggressorName.IsEnabled = false;
            tbCopmlainantName.IsEnabled = false;
            tbAggressorCi.IsEnabled = true;
            tbVictimCi.IsEnabled = true;
            tbComplainantCi.IsEnabled = true;
        }

        private void VerifyPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if(Regex.IsMatch(e.Text.ToString(),"^[A-Za-z´áéíóúAÉÍÓÚÑñ ]+$") && (sender as AutoCompleteBox).Text.Length<50)
                e.Handled = false;
            else
                e.Handled = true;
        }
        private void VerifyPreviewNumberTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Regex.IsMatch(e.Text.ToString(), "^[0-9]+$") && (sender as AutoCompleteBox).Text.Length < 10)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void dtEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dtStart.DisplayDateEnd = dtEnd.SelectedDate;
            FilterResults();
        }

        private void dtStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dtEnd.DisplayDateStart = dtStart.SelectedDate;
            FilterResults();
        }

        private void btnResetDates_Click(object sender, RoutedEventArgs e)
        {
            dtStart.SelectedDate = null;
            dtEnd.SelectedDate = DateTime.Now;
            FilterResults();
        }

        private void listViewComplaints_MouseUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var complaintInformation = listViewComplaints.SelectedItem as ComplaintInformation;
                if (complaintInformation != null)
                {
                    SelectedComplaintId = complaintInformation.NumberOfCase;
                     var window=Window.GetWindow(this);
                    if(window is SearchComplaintWindow)
                        window.Close();
                }    
            }
            catch
            {
                //MessageBox.Show("No se pudo obtener la denuncia intente nuevamente", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void tbCasxteNumber_DropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            ComplaintInformation comp = tbCaseNumber.SelectedItem as ComplaintInformation;
            if (comp != null)
            {
                FilterResults();
            }
        }

        private void TbTextChanged(object sender, RoutedEventArgs e)
        {
            var tbox = (sender as AutoCompleteBox);
            if (String.IsNullOrWhiteSpace(tbox.Text))
            {
                if (tbox == tbVictimName)
                {
                    tbVictimCi.Text = "";
                    VictimId = null;
                }
                if (tbox == tbVictimCi)
                {
                    tbVictimName.Text = "";
                    VictimId = null;
                }
                if (tbox == tbAggressorName)
                {
                    tbAggressorCi.Text = "";
                    AggressorId = null;
                }
                if (tbox == tbAggressorCi)
                {
                    tbAggressorName.Text = "";
                    AggressorId = null;
                }
                if (tbox == tbCopmlainantName)
                {
                    tbComplainantCi.Text = "";
                    ComplainantId = null;
                }
                if (tbox == tbComplainantCi)
                {
                    tbCopmlainantName.Text = "";
                    ComplainantId = null;
                }
                FilterResults();
            }
        }

        private void AdvancedOptions_Expanded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation(25, 206, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
            Storyboard st = new Storyboard();
            st.Children.Add(doubleAnimation);
            Storyboard.SetTargetProperty(st, new PropertyPath("Height"));
            Storyboard.SetTargetName(st, "AdvancedOptions");
            BeginStoryboard(st);
        }

        private void AdvancedOptions_Collapsed(object sender, RoutedEventArgs e)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation(206, 25, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
            Storyboard st = new Storyboard();
            st.Children.Add(doubleAnimation);
            Storyboard.SetTargetProperty(st, new PropertyPath("Height"));
            Storyboard.SetTargetName(st, "AdvancedOptions");
            BeginStoryboard(st);
        }

    }
}
